//
//  WorkController.m
//  RelationshipIPhone
//
//  Created by Vladimir Oboroc on 11/19/12.
//  Copyright (c) 2012 Vladimir Oboroc. All rights reserved.
//

#import <AVFoundation/AVFoundation.h>
#import "WorkController.h"


//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
@implementation WorkController
{
    AVCaptureSession   *videoSession;
    CLLocationManager  *headingManager;
}

//----------------------------------------------------------------------------------------------------------------------
-(void)viewWillAppear:(BOOL)animated
{
    if (!videoSession)
    {
        [self createVideoSession];
    }
    
    if (!headingManager)
    {
        [self createHeadingManager];
    }
    
	[videoSession startRunning];
    [headingManager startUpdatingLocation];
    [headingManager startUpdatingHeading];
}

//----------------------------------------------------------------------------------------------------------------------
-(void)viewWillDisappear:(BOOL)animated
{
    [videoSession stopRunning];
    [headingManager stopUpdatingLocation];
    [headingManager stopUpdatingHeading];
}

//----------------------------------------------------------------------------------------------------------------------
-(void)createVideoSession
{
    videoSession = [[AVCaptureSession alloc] init];
    
	AVCaptureVideoPreviewLayer *captureLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:videoSession];
	AVCaptureDevice            *device       = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
	NSError                    *error        = nil;
	AVCaptureDeviceInput       *input        = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    
	videoSession.sessionPreset = AVCaptureSessionPresetMedium;
    
	captureLayer.frame        = self.view.bounds;
    captureLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    
	[self.view.layer addSublayer:captureLayer];
    
	if (!input)
    {
		// Handle the error appropriately.
		NSLog(@"ERROR: trying to open camera: %@", error);
        
        videoSession = nil;
	}
    else
    {
        [videoSession addInput:input];
    }
}

//----------------------------------------------------------------------------------------------------------------------
-(void)createHeadingManager
{
    headingManager = [[CLLocationManager alloc] init];
    
    headingManager.delegate = self;    
}

#pragma mark - CLLocationManagerDelegate
//----------------------------------------------------------------------------------------------------------------------
-(void)locationManager:(CLLocationManager*)manager didUpdateHeading:(CLHeading*)newHeading
{
    NSLog(@"-- %f", newHeading.trueHeading);
    NSLog(@"++ %f", newHeading.magneticHeading);
}

//----------------------------------------------------------------------------------------------------------------------
@end

//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
